package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.CategoryDao;
import com.example.domain.Category;
import com.example.domain.DataJson;
import com.example.domain.TableData;
import com.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public TableData<Category> getPage(Integer page, Integer limit, String cateName) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(cateName), "cate_name", cateName);

        IPage<Category> ipage = new Page(page, limit);
        IPage<Category> categoryIPage = categoryDao.selectPage(ipage, queryWrapper);
        TableData<Category> tableData = new TableData<>();
        tableData.setCount(categoryIPage.getTotal());
        tableData.setCode(0);
        tableData.setMsg("");

        tableData.setData(categoryIPage.getRecords());
        return tableData;
    }

    //    @Override
//    public DataJson updateCategory(Integer id, String imagePath, String name) {
//        Category category = categoryDao.selectById(id);
//        category.setCateIcon(imagePath);
//        category.setCateName(name);
//        categoryDao.updateById(category);
//
//        DataJson dataJson = new DataJson();
//        dataJson.setCode(0);
//        dataJson.setMsg("修改成功");
//
//        return dataJson;
//    }
    @Override
    public DataJson updateCategory(Category category) {
        Category cate = categoryDao.selectById(category.getId());
        cate.setCateIcon(category.getCateIcon());
        cate.setCateName(category.getCateName());
        categoryDao.updateById(cate);

        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        dataJson.setMsg("修改成功");

        return dataJson;
    }
}
